<?php
core::load_class('init', 'admin');
class order_control extends init_control {
	public function _initialize() {
		parent::_initialize();
		$this->service = model('order', 'service');
	}

	public function index(){
		$field = "SUM(real_amount) sum,count(distinct member_id) as member_num,count(id) as order_num";
		$sqlmap['_string']='date_format(from_UNIXTIME(`create_time`),\'%Y-%m-%d\') = date_format(now(),\'%Y-%m-%d\')';
		$sqlmap['status']=1;
		$data = $this->service->_query($field,$sqlmap,$group);
		$data = current($data);
		//取消订单数量
		$field = "count(id) as pass_order_num";
		$sqlmap['_string']='date_format(from_UNIXTIME(`create_time`),\'%Y-%m-%d\') = date_format(now(),\'%Y-%m-%d\')';
		$sqlmap['status']=array('GT',1);
		$passdata = $this->service->_query($field,$sqlmap,$group);
		$passdata = current($passdata);
		$data['passdata'] = (int)$passdata['passdata'];	
		
		include $this->admin_tpl('order');
	}

	public function ajax_getdata(){
		if(empty($_GET['formhash']) || $_GET['formhash'] != FORMHASH) showmessage('_TOKEN_ERROR_');
		$params = $_GET;
		$sqlmap = array();
		$xAxis = array();
		/* 时间周期 */
		if(isset($params['days']) && $params['days']>0){
			$params['etime'] = time();
			$params['stime'] = strtotime("-{$params['days']}day",$params['etime']);
			
		}
		if(isset($params['stime']{0}) && isset($params['etime']{0})){
			$params['etime'] = strtotime($params['etime']);
			$params['stime'] = strtotime($params['stime']);
		}
		
		$days=round(($params['etime']-$params['stime'])/86400);
		
		//两个时间戳之间的天数
		
		$sqlmap['create_time'] = array('between',array(
			strtotime(date('Y-m-d',$params['stime']).'00:00:00'),
			strtotime(date('Y-m-d',$params['etime']).'23:59:59')
		));
		$group = 'days';
		$subtext = $params['stime'].'-'.$params['etime'];
		for ($i=0; $i <= $days; $i++) { 
			$xAxis[$i] = date('Y-m-d',strtotime("+{$i}day",$params['stime']));
		}
		//交易额
		$field = "FROM_UNIXTIME(create_time,'%Y-%m-%d') days,SUM(real_amount) sum,count(distinct member_id) as member_num,count(id) as order_num";
		$_sale = $this->service->_query($field,$sqlmap,$group);
	
		foreach($_sale as $k =>$v){
			$_sale[$v['days']] = $_sale[$k];
		}
		//地区
		$field = "delivery_address_district as area ,count(delivery_address_district) as areanum";
		$group = 'area';
		$_area = $this->service->_query($field,$sqlmap,$group);
		foreach($_area as $k =>$v){
			$r = model('admin/district','service')->fetch_parents($v['area']);
			$area_arr=end($r);
			if(isset($areaval[$area_arr['id']])){
				$areaval[$area_arr['id']]['areanum'] += $_area[$k]['areanum'];
			}else{
				$areaval[$area_arr['id']] = $_area[$k];
			}	
		}
		//支付方式
		$field = "pay_method as pay ,count(pay_method) as paynum";
		$group = 'pay';
		$payment = array('alipay','alipay_escow','bank','ws_wap','wechat_js','wechat_qr');
		//在线支付
		$sqlmap['delivery_method']=1;
		$_pay = $this->service->_query($field,$sqlmap,$group);
		//货到付款
		$sqlmap['delivery_method']=2;
		$_pay_delivery = $this->service->_count($field,$sqlmap);
		
		unset($sqlmap['delivery_method']);
		
		foreach($_pay as $k =>$v){
			$_pay[$v['pay']] = $_pay[$k]['paynum'];
		}
		
		//组装交易数据
		foreach ($xAxis as $key => $value) {
			$_saleval[] = isset($_sale[$value]['sum'])?$_sale[$value]['sum']:'0.00';
			$_orderval[] = isset($_sale[$value]['order_num'])?$_sale[$value]['order_num']:'0';
			$_priceval[] =  sprintf("%.2f", (int)$_sale[$value]['sum']/(int)$_sale[$value]['member_num']);
		}
		//组装地区数据
		for ($i=1; $i <= 33; $i++) { 
			$_areaval[] = isset($areaval[$i]['areanum'])?(int)$areaval[$i]['areanum']:0;
		}
		//组装交易数据
		foreach ($payment as $key => $value) {
			$_payval[$value] = isset($_pay[$value])?(int)$_pay[$value]:0;
		}
		$_payval['delivery'] = $_pay_delivery;
		
		$row['sale'] ['xAxis']= $xAxis;
		$row['sale'] ['series'][]= $_saleval;
		$row['sale'] ['series'][]= $_orderval;
		$row['sale'] ['series'][]= $_priceval;
		
		$row['area'] ['max']= max($_areaval);
		$row['area'] ['series'][]= $_areaval;
		
		$row['pay'] ['series']= $_payval;
		echo json_encode($row);
	}
}